#include <stdio.h>
#include <boost/preprocessor/arithmetic/add.hpp>
#include <boost/preprocessor/arithmetic/sub.hpp>
#include <boost/preprocessor/comparison/less_equal.hpp>
#include <boost/preprocessor/control/while.hpp>
#include <boost/preprocessor/list/adt.hpp>
#include <boost/preprocessor/tuple/elem.hpp>

typedef struct linear_fib_state {
   int a0, a1, n;
} linear_fib_state;

static int linear_fib_c(linear_fib_state p) {
   return p.n;
}

static linear_fib_state linear_fib_f(linear_fib_state p) {
   linear_fib_state r = { p.a1, p.a0 + p.a1, p.n - 1 };
   return r;
}

static int linear_fib(int n) {
   linear_fib_state p = { 0, 1, n };
   while (linear_fib_c(p)) {
      p = linear_fib_f(p);
   }
   return p.a0;
}

#define LINEAR_FIB(n) LINEAR_FIB_D(1, n)

#define LINEAR_FIB_D(d, n) \
   BOOST_PP_TUPLE_ELEM(3, 0, BOOST_PP_WHILE_ ## d(LINEAR_FIB_C, LINEAR_FIB_F, (0, 1, n)))

#define LINEAR_FIB_C(d, p) \
   /* p.n */ BOOST_PP_TUPLE_ELEM(3, 2, p) \
   /**/

#define LINEAR_FIB_F(d, p) \
   ( \
			 BOOST_PP_TUPLE_ELEM(3, 1, p), \
			 BOOST_PP_ADD_D(d, BOOST_PP_TUPLE_ELEM(3, 0, p), BOOST_PP_TUPLE_ELEM(3, 1, p)), \
			 BOOST_PP_DEC(BOOST_PP_TUPLE_ELEM(3, 2, p)) \
   ) \
   /**/

int main() {
   printf("linear_fib(10) = %d\n", linear_fib(10));
   printf("LINEAR_FIB(10) = %d\n", LINEAR_FIB(10));
   return 0;
}
#define D = vgyhb;
#include <stdio.h>
#include <boost/preprocessor/arithmetic/add.hpp>